﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using DevExpress.XtraCharts;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class rpt_F5DailyTime : DevExpress.XtraReports.UI.XtraReport
    {
        private DataSet sourceDataSet;
        public string staffid;
        public rpt_F5DailyTime()
        {
            InitializeComponent();
            sourceDataSet = new DataSet();
            this.staffid = null;
        }

        public void BindData()
        {
            planNo.DataBindings.Add("Text", DataSource, "PlanNo");            
            partNo.DataBindings.Add("Text", DataSource, "PartNo");
            needTime.DataBindings.Add("Text", DataSource, "Needtime");
            usedTime.DataBindings.Add("Text", DataSource, "Used_Time");
            orderNo.DataBindings.Add("Text", DataSource, "OrderNo");

            // Specify data members to bind the series.
            f5DailyTimeChart.Series["Need Time"].DataSource = sourceDataSet.Tables[0];
            f5DailyTimeChart.Series["Need Time"].ArgumentScaleType = ScaleType.Auto;
            f5DailyTimeChart.Series["Need Time"].ArgumentDataMember = "PartName";
            f5DailyTimeChart.Series["Need Time"].ValueScaleType = ScaleType.Numerical;
            f5DailyTimeChart.Series["Need Time"].ValueDataMembers.AddRange(new string[] { "NeedTime" });

            f5DailyTimeChart.Series["Used Time"].DataSource = sourceDataSet.Tables[0];
            f5DailyTimeChart.Series["Used Time"].ArgumentScaleType = ScaleType.Auto;
            f5DailyTimeChart.Series["Used Time"].ArgumentDataMember = "PartName";
            f5DailyTimeChart.Series["Used Time"].ValueScaleType = ScaleType.Numerical;
            f5DailyTimeChart.Series["Used Time"].ValueDataMembers.AddRange(new string[] { "Used_Time" });

            f5DailyTimeChart.Titles[1].Text = "Update Date: " 
                + DateTime.Now.Date.ToString("yyyy/MM/dd") +"    By: " + this.staffid;
        }

        public void LoadData()
        {           
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT t1.PlanNo, t2.PartNo, t1.CommandQuantity*t2.AssSTDTime AS Needtime,"
                + " SUM(t3.AssemblyTime) AS Used_Time, t1.[OrderNo],"
                + " CASE WHEN LEFT(t2.PartNo, 3) = 'ASZ' THEN LEFT(t2.PartNo, 4) + ' - ' + t1.PlanNo ELSE 'C' + ' - ' + t1.PlanNo END AS PartName"
                + " FROM ([231_F5Plan] AS t1 INNER JOIN"
                + " [222_Part] AS t2 ON t1.PartNo = t2.PartNo) INNER JOIN"
                + " [231_TimeInput] AS t3 ON t1.[OrderNo] = t3.TimePlanNo"
                + " GROUP BY t1.PlanNo, t2.PartNo, t1.CommandQuantity*t2.AssSTDTime, t1.[OrderNo], t1.FinishedDate"
                + " HAVING ((t1.PlanNo Like 'L%') AND (t1.FinishedDate IS NULL))";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                try
                {
                    this.DataSource = sourceDataSet.Tables[0];                    
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }
    }
}
